package com.fruit.audit.service.impl;

import java.sql.Timestamp;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.fruit.audit.bean.TutorUser;
import com.fruit.audit.dao.ITutorUserDao;
import com.fruit.audit.exception.FruitException;
import com.fruit.audit.exception.FruitExceptionEnum;
import com.fruit.audit.service.ITutorUserService;
import com.fruit.tool.util.MD5Util;

/**
 * 用户 逻辑
 * @author jinghao.liang
 *
 */
@Service("tutorUserService")
public class TutorUserService implements ITutorUserService{
	@Resource(name = "tutorUserDao")
	private ITutorUserDao tutorUserDao;

	public TutorUser loginUser(String phone, String password) {
		
		TutorUser user = new TutorUser();
		
		user = tutorUserDao.detailByPhone(phone);
		
		if(user == null) {
			throw new FruitException(FruitExceptionEnum.USER_WRONG_EXCEPTION);
		}
		
		if (!user.getPassword().equals(password)) {
			throw new FruitException(FruitExceptionEnum.PASSWORD_WRONG_EXCEPTION );
		}
		
	/*	if(user.getStatus() == UserStatus.DELETE.getValue()) {
			throw new FruitException(FruitExceptionEnum.USER_DELETE_EXCEPTION);
		}*/
		
		String md5Phone = MD5Util.GetMD5Code(phone + System.currentTimeMillis(), true, false);
		String md5Password = MD5Util.GetMD5Code(password + System.currentTimeMillis(), true, false);
		String token = md5Phone + "-" + md5Password;
		user.setToken(token);
		tutorUserDao.updateLoginUser(token, new Timestamp(System.currentTimeMillis()), user.getId());
		
		return user;
	}

	@Override
	public void loginOut(String token) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public TutorUser validUser(String token, Integer i) {
		// TODO Auto-generated method stub
		return null;
	}
}
